package com.hetao.xx;


import android.app.Application;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.hetao.xx.game.model.User;
import com.hetao.xx.orm.GoodsDao;
import com.hetao.xx.orm.NpcDao;
import com.hetao.xx.orm.NpcEventDao;
import com.hetao.xx.orm.PlaceDao;
import com.hetao.xx.orm.PlaceNpcDao;
import com.hetao.xx.orm.UserDao;
import com.hetao.xx.orm.UserGoodsDao;
import com.hetao.xx.service.FightManager;
import com.hetao.xx.service.MessageManager;
import com.hetao.xx.service.PlaceManager;

public class Global {

    private static Global instant;

    public static Global getInstant() {
       if (instant == null) {
           instant = new Global();
       }
        return instant;
    }

    public void init(Application application) {
        this.application = application;
        user = new User();
        msgManage = new MessageManager();
        placeManager = new PlaceManager();
        fightManager = new FightManager(this);

        userDao = new UserDao(application);
        goodsDao = new GoodsDao(application);
        userGoodsDao = new UserGoodsDao(application);
        npcDao = new NpcDao(application);
        placeDao = new PlaceDao(application);
        placeNpcDao = new PlaceNpcDao(application);
        npcEventDao = new NpcEventDao(application);
    }


    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;

    }

    private Application application;

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }

    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void notifyMsg(String msg) {
        if (handler == null) return;
        Message message = Message.obtain();
        message.what = constants.MainNotifyMsg;
        message.obj = msg;
        handler.dispatchMessage(message);

        msgManage.addMsg(msg);
    }


    public void updateSite(String site) {
        if (handler == null) return;
        Message message = Message.obtain();
        message.what = constants.MainUpdateSite;
        message.obj = site;
        handler.dispatchMessage(message);
    }

    public void switchMainPage(int fragment, int placeId, Object object) {
        if (handler == null) return;
        Message message = Message.obtain();
        message.what = constants.MainSwitchPage;
        message.arg1 = fragment;
        message.arg2 = placeId;
        message.obj = object;
        handler.dispatchMessage(message);
    }

    public void updateUserInfo() {
        if (handler == null) return;
        Message message = Message.obtain();
        message.what = constants.MainUpdateMine;
        handler.dispatchMessage(message);
    }

    public void toastMsg(String msg) {
        Toast.makeText(application, msg, Toast.LENGTH_SHORT).show();
        msgManage.addMsg(msg);
    }

    public MessageManager msgManage;
    public PlaceManager placeManager;
    public FightManager fightManager;



    //dao
    public UserDao userDao;
    public GoodsDao goodsDao;
    public UserGoodsDao userGoodsDao;
    public NpcDao npcDao;
    public PlaceNpcDao placeNpcDao;
    public PlaceDao placeDao;
    public NpcEventDao npcEventDao;


}
